from flask import Blueprint

from apps.a_common.db import safe_commit
from apps.a_common.log import MyLogger
from apps.a_common.permission import has_permission_decorate
from apps.a_common.request import assert_params_by_form
from apps.a_common.response import success_response, paginate_response
from apps.foundation import db
from apps.users.forms import PermissionGroupEditorForm
from apps.users.models import Permission, PermissionGroup, User
from apps.users.serializers import serialize_permission_group

permission_group_blue_print = Blueprint('permission_group', __name__)
log = MyLogger('permission group view')


@permission_group_blue_print.route('/permission-group/<int:permission_group_id>/', methods=['GET'])
@has_permission_decorate('admin-permission-group-view')
def _get_one_permission(permission_group_id):
    """
    单个权限组详情
    ---
    tags:
      - permission-group
    parameters:
      - name: permission_group_id
        in : path
        type: integer
        required: true
        description: 权限的id
    """
    group = PermissionGroup.query.filter(PermissionGroup.id == permission_group_id).first()
    permissions = Permission.query.filter(Permission.id.in_(group.permission_id_list)).all()
    users = User.query.filter(User.id.in_(group.user_id_list)).all()
    return success_response(serialize_permission_group(group, users, permissions))


@permission_group_blue_print.route('/permission-group/', methods=['GET'])
@has_permission_decorate('admin-permission-group-view')
def _get_permissions():
    """
    获得权限组列表
    ---
    tags:
      - permission-group
    parameters:
      - name: page_id
        in : query
        type: integer
        required: false
        default: 1
        description: 页码
      - name: page_size
        in : query
        type: integer
        required: false
        default: 20
        description: 每页的大小
    """
    queryset = PermissionGroup.query
    return paginate_response(queryset, serialize_permission_group)


@permission_group_blue_print.route('/permission-group/', methods=['POST'])
@has_permission_decorate('admin-permission-group-add')
def _post_permission():
    """
    新增权限组
    ---
    tags:
      - permission-group
    parameters:
      - name: name
        in : formData
        type: string
        required: true
        description: 权限的名字
      - name: user_id_list
        in : formData
        type: array
        required: true
        description: 权限的类型
      - name: permission_id_list
        in : formData
        type: array
        required: true
        description: 权限的类型
    """
    form = assert_params_by_form(PermissionGroupEditorForm)
    log.info(form)
    group = PermissionGroup(name=form['name'], user_id_list=form['user_id_list'],
                            permission_id_list=form['permission_id_list'])
    db.session.add(group)

    permissions = Permission.query.filter(Permission.id.in_(group.permission_id_list)).all()
    users = User.query.filter(User.id.in_(group.user_id_list)).all()
    safe_commit()
    return success_response(serialize_permission_group(group, users, permissions))


@permission_group_blue_print.route('/permission-group/<int:permission_group_id>/', methods=['PUT'])
@has_permission_decorate('admin-permission-group-change')
def _put_permission(permission_group_id):
    """
    修改权限组
    ---
    tags:
      - permission-group
    parameters:
      - name: permission_group_id
        in : path
        type: integer
        required: true
        description: 权限组的id
      - name: name
        in : formData
        type: string
        required: true
        description: 权限的名字
      - name: user_id_list
        in : formData
        type: array
        required: true
        description: 权限的类型
      - name: permission_id_list
        in : formData
        type: array
        required: true
        description: 权限的类型
    """
    form = assert_params_by_form(PermissionGroupEditorForm)
    group = PermissionGroup.query.filter(PermissionGroup.id == permission_group_id).first()
    group.name = form['name']
    group.user_id_list = form['user_id_list']
    group.permission_id_list = form['permission_id_list']

    permissions = Permission.query.filter(Permission.id.in_(group.permission_id_list)).all()
    users = User.query.filter(User.id.in_(group.user_id_list)).all()
    safe_commit()
    return success_response(serialize_permission_group(group, users, permissions))


@permission_group_blue_print.route('/permission-group/<int:permission_group_id>/', methods=['delete'])
@has_permission_decorate('admin-permission-group-delete')
def _delete_permission(permission_group_id):
    """
    删除权限组
    ---
    tags:
      - permission-group
    parameters:
      - name: permission_group_id
        in : path
        type: integer
        required: true
        description: 权限的id
    """
    group = PermissionGroup.query.filter(PermissionGroup.id == permission_group_id).first()
    db.session.delete(group)

    permissions = Permission.query.filter(Permission.id.in_(group.permission_id_list)).all()
    users = User.query.filter(User.id.in_(group.user_id_list)).all()
    safe_commit()
    return success_response(serialize_permission_group(group, users, permissions))
